exports.addAboutCourse = function (req, res) {

    var bodyParams = req.routeInfo.bodyParams;

    //任务流创建
    async.waterfall([

        //查看是或否已经约过该课程了
        function(cb){
            R.SEND_HTTP(
                req,
                {
                    url:CORE_APIConfig.coreAPI.searchAppointmentRecord+"/ar_cid/"+bodyParams.ar_cid+"/ar_uid/"+bodyParams.ar_uid+"/ar_oid/"+bodyParams.ar_oid,
                    method:"POST",
                    data : {
                        fields: ["ar_id"].join(",")
                    }
                },
                function (err, data) {
                    //console.log(data.data);
                    if(data.data.length>0){
                        return RES.response(res, false, "抱歉，您已经约过此课程！");
                    }else{
                        cb()
                    }
                }
            )

        },

        //添加约课记录
        function(cb){
            
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.addAppointmentRecord,
                    method: 'POST',
                    data : {
                        ar_uid   : bodyParams.ar_uid,
                        ar_name  : bodyParams.ar_name,
                        ar_phone : bodyParams.ar_phone,
                        ar_des   : bodyParams.ar_des,
                        ar_cid   : bodyParams.ar_cid,
                        ar_oid   : bodyParams.ar_oid
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return RES.response(res, false, data);
                    }

                    cb();
                }
            );

        },

        //发送成功短信
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.sendCodeSms,
                    method: 'POST',
                    data  : {
                        phone   : bodyParams.ar_phone,
                        type    : 4,
                        o_name : bodyParams.o_name,

                    }
                },
                function (err, data) {
                    //成功发送私信
                    R.SEND_HTTP(
                        req,
                        {
                            url: "http://127.0.0.1:3400/weixin/templateMessagesSent",
                            method: 'POST',
                            data  : {
                                openid : bodyParams.openid,
                                clickurl:bodyParams.clickurl,//详情链接
                                type   : "yueKeChengGong",
                                courseOrganization:bodyParams.o_name,//机构名称
                                courseTitle:bodyParams.ar_cname,//课程名称，
                               // curriculumTime:bodyParams.curriculumTime,//开课时间
                                contactWay : bodyParams.ar_ophone,//联系方式
                            }
                        },
                        function (err, data) {

                           cb();

                        }
                    );

                }
            );


        },
        //查询机构管理员的openid
        function(cb) {
            R.SEND_HTTP(
                req, {
                    url: CORE_APIConfig.coreAPI.searchOrganizationUser + "/ou_oid/"+ bodyParams.ar_oid,
                    method: 'POST',
                    data: {
                        fields:[
                            "openid",//机构管理员的openid
                        ].join(",")
                    }
                },
                function (err, data) {
                    open = data.data;
                    cb();
                }
            );

        },
        //循环发送私信
        function (cb) {
            var tasks = [];
            open.forEach(function (item, i) {
                if(item.openid != null && item.openid != ''){
                    tasks.push(function (cb) {
                    	
                        R.SEND_HTTP(
                            req,
                            {
//                              url: "http://manage.artmofang.com:3400/weixin/templateMessagesSent",
                                url: "http://127.0.0.1:3200/weixin/templateMessagesSent",
                                method: 'POST',
                                data  : {
                                    openid : item.openid,
                                    type    : "yueKeShenQingChuLi",
                                    userName : bodyParams.ar_name, //学员名称
                                    voucherName:bodyParams.ar_cname, //课程名称
                                    voucherMoney:0, //订单金额
                                    phone : bodyParams.ar_phone,//联系方式
                                    remark : des,
                                }
                            },
                            function (err, data) {

                                cb();

                            }
                        );

                    })
                }
            });
            async.waterfall(tasks, cb);
        }
            
       
    ], function (err, result) {

        //返回最终拼接结果
        RES.response(res, true);

    });

}